package com.k.kpp.biz.datasource.controller;

import com.k.kpp.biz.datasource.model.DataSourceSettingEntity;
import com.k.kpp.biz.datasource.model.DataSourceSettingVo;
import com.k.kpp.biz.datasource.model.JdbcConnEntity;
import com.k.kpp.config.datasource.DynamicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author k
 */
@Controller
public class DataSourceSettingController {
    private final static Logger LOGGER = LoggerFactory.getLogger(DataSourceSettingController.class);
    @Autowired
    @Qualifier(value = "dynamicDataSource")
    private DynamicDataSource dynamicDataSource;

    @RequestMapping("/dataSourceSetting/addPage")
    public String addPage(){
        LOGGER.debug("/dataSourceSetting/add");
        return "setting/datasource";
    }

    @RequestMapping("/dataSourceSetting/list")
    public DataSourceSettingVo list(){
        LOGGER.debug("/dataSourceSetting/list");
        return null;
    }

    @RequestMapping("/dataSourceSetting/saveOrUpdate")
    @ResponseBody
    public Map<String,Object> saveOrUpdate(@RequestBody DataSourceSettingVo vo){
        LOGGER.debug("/dataSourceSetting/saveOrUpdate");
        try {
            dynamicDataSource.saveOrUpdateDataSource("master",buildJdbcMap(vo,true));
            dynamicDataSource.saveOrUpdateDataSource("slave",buildJdbcMap(vo,false));
        } catch (SQLException e) {
            LOGGER.error("err:",e);
            e.printStackTrace();
        }
        return null;
    }

    private Map<String,Object> buildJdbcMap(DataSourceSettingVo vo, boolean isMaster) {
        if(isMaster){
            return buildJdbcInfoMap(vo.getDbTypename(),vo.getMasterIp(),vo.getMasterPort(),vo.getMasterDatabaseName(),vo.getMasterAccount(), vo.getMasterPassword());
        } else {
            return buildJdbcInfoMap(vo.getDbTypename(),vo.getSlaveIp(),vo.getSlavePort(),vo.getSlaveDatabaseName(),vo.getSlaveAccount(),vo.getSlavePassword());
        }
    }

    private Map<String,Object> buildJdbcInfoMap(String dbTypename, String dbIp, String dbPort, String dbName,String dbUsername,String dbPassword) {
        Map<String,Object> cfg = new HashMap<>();
        String jdbcUrl = "";
        String driverClassName = "";
        if ("ORACLE".equalsIgnoreCase(dbTypename)) {
            driverClassName = "oracle.jdbc.driver.OracleDriver";
            jdbcUrl = "jdbc:oracle:thin:@"+dbIp+":"+dbPort+":"+dbName;
        } else if ("MYSQL".equalsIgnoreCase(dbTypename)) {
            driverClassName = "com.mysql.jdbc.Driver";
            jdbcUrl="jdbc:mysql://"+dbIp+":"+dbPort+"/"+dbName+"?useUnicode=true&amp;characterEncoding=utf-8";
        } else if("POSTGRESQL".equalsIgnoreCase(dbTypename)){
            driverClassName = "org.postgresql.Driver";
            jdbcUrl = "jdbc:postgresql://"+dbIp+":"+dbPort+"/"+dbName;
        }else if ("SQLSERVER".equalsIgnoreCase(dbTypename)) {
            driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            jdbcUrl = "jdbc:sqlserver://"+dbIp+":"+dbPort+";databaseName="+dbName+";";
        }else if("sqlite".equalsIgnoreCase(dbTypename)){
            driverClassName = "org.sqlite.JDBC";
            //jdbcUrl = "jdbc:sqlite::resource:static/sqllite/"+entity.getDbName();
            // jdbc:sqlite://c:/sqlite/test.db
            jdbcUrl = "jdbc:sqlite::resource:data/sqlite/"+dbName;
        } else if("MYSQL".equalsIgnoreCase(dbTypename)) {
            driverClassName = "com.mysql.jdbc.Driver";
            jdbcUrl = "jdbc:mysql://"+dbIp+":"+dbPort+"/"+dbName;
        } else {
            driverClassName = "org.sqlite.JDBC";
            //"jdbc:sqlite::resource:static/sqllite/ucms.db";
            jdbcUrl = "jdbc:sqlite::resource:data/sqlite/sql.db";
        }
        cfg.put("jdbcUrl",jdbcUrl);
        cfg.put("driverClassName",driverClassName);
        cfg.put("username",dbUsername);
        cfg.put("password",dbPassword);
        return cfg;
    }


    private JdbcConnEntity buildJdbcConnInfo(DataSourceSettingEntity entity){
        JdbcConnEntity connInfo = new JdbcConnEntity();
        String dbType = entity.getDbType();
        String jdbcUrl = "";
        String driverClassName = "";
        String username = entity.getDbUsername();
        String password = entity.getDbPassword();
        if ("ORACLE".equals(dbType)) {
            driverClassName = "oracle.jdbc.driver.OracleDriver";
            jdbcUrl = "jdbc:oracle:thin:@"+entity.getDbIp()+":"+entity.getDbPort()+":"+entity.getDbName();
        } else if ("MYSQL".equals(dbType)) {
            driverClassName = "com.mysql.jdbc.Driver";
            jdbcUrl="jdbc:mysql://"+entity.getDbIp()+":"+entity.getDbPort()+"/"+entity.getDbName()+"?useUnicode=true&amp;characterEncoding=utf-8";
        } else if("POSTGRESQL".equals(dbType)){
            driverClassName = "org.postgresql.Driver";
            jdbcUrl = "jdbc:postgresql://"+entity.getDbIp()+":"+entity.getDbPort()+"/"+entity.getDbName();
        }else if ("SQLSERVER".equals(dbType)) {
            driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            jdbcUrl = "jdbc:sqlserver://"+entity.getDbIp()+":"+entity.getDbPort()+";databaseName="+entity.getDbName()+";";
        }else if("sqlite".equalsIgnoreCase(dbType)){
            driverClassName = "org.sqlite.JDBC";
            //jdbcUrl = "jdbc:sqlite::resource:static/sqllite/"+entity.getDbName();
            // jdbc:sqlite://c:/sqlite/test.db
            jdbcUrl = "jdbc:sqlite::resource:data/sqlite/"+entity.getDbName();
        } else if("MYSQL".equalsIgnoreCase(dbType)) {
            driverClassName = "com.mysql.jdbc.Driver";
            jdbcUrl = "jdbc:mysql://"+entity.getDbIp()+":"+entity.getDbPort()+"/"+entity.getDbName();
        } else {
            driverClassName = "org.sqlite.JDBC";
            //"jdbc:sqlite::resource:static/sqllite/ucms.db";
            jdbcUrl = "jdbc:sqlite::resource:data/sqlite/sql.db";
        }
        connInfo.setJdbcUrl(jdbcUrl);
        connInfo.setDriverClassName(driverClassName);
        connInfo.setUsername(username);
        connInfo.setPassword(password);
        return connInfo;
    }
}
